import React from 'react';
import { connect } from 'dva'
import { createForm } from 'rc-form';
import { Icon, List, WhiteSpace, Button,Stepper,Picker,DatePicker,InputItem, Toast  } from 'antd-mobile';
import styles from './addappraisal.less'

const nowTimeStamp = Date.now();
const now = new Date(nowTimeStamp);
const utcOffset = new Date(now.getTime() - (now.getTimezoneOffset() * 60000));
const Item = List.Item;
const orgLists = [];
const penalty = [];
const resultsList = [];
const years = [];
let menberListss=[];
class addappraisal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      years: [],
      sValue: [],
      sdengci: [],
      sresults: [],
      menber: [],
      org: [],
      date: now,
      idt: utcOffset.toISOString().slice(0, 10),
    }
    this.props.dispatch({
      type: 'organizationss/orgBottom',
      payload: {},
    });
  }
  orgClick = (v) => {
    console.log("value",v);
    menberListss=[];
    this.setState({menber:[]})
    new Promise((resolve, reject) => {
      this.props.dispatch({
        type: 'partybuildings/fetch',
        payload: {
          org_code: v,
          resolve, reject
        }
      });
    }).then(res => {
      if (res.data.code === 200) {
        const menberData = res.data.ret;
        if (menberListss.length < 1) {
          for (let key in menberData) {
            menberListss.push({label: menberData[key].name, value: menberData[key].code})
          }
          this.setState({
            a:1
          })
        }
      }
      else {
        Toast.success('查询出错！', 1);
      }
    });
  };

  onSubmit = () => {
    this.props.form.validateFields({force: true}, (error) => {
      if (!error) {
        this.props.dispatch({
          type: 'addappraisals/fetch',
          payload: {
            year: this.state.years[0],
            review_time: this.state.idt,
            review_level: this.state.sresults[0],
            bonus_penalty: this.state.sdengci[0],
            org_zb_code: this.state.org[0],
            member_code: [this.state.menber[0]],
          },
        });
      } else {
        console.log(error);
      }
    });
  }
  validateIdp = (rule, date, callback) => {
    if (isNaN(Date.parse(date))) {
      callback(new Error('日期格式不正确'));
    } else {
      const cDate = new Date(date);
      const newDate = new Date(+this.state.dpValue);
      newDate.setFullYear(cDate.getFullYear());
      newDate.setMonth(cDate.getMonth());
      newDate.setDate(cDate.getDate());
      // this.setState({ dpValue: newDate });
      setTimeout(() => this.props.form.setFieldsValue({dp: newDate}), 10);
      callback();
    }
  }

  render() {
    if (this.props.orgList) {
      const orgData = this.props.orgList;
      if (orgLists.length < 1) {
        for (let key in orgData) {
          orgLists.push({label: orgData[key].name, value: orgData[key].zb_code})
        }
      }
    }
    if (this.props.list) {
      const penaltyData = this.props.list;
      if (penalty.length < 1) {
        for (let key in penaltyData) {
          penalty.push({label: penaltyData[key].name, value: penaltyData[key].code})
        }
      }
    }

    if (this.props.results) {
      const resultsData = this.props.results;
      if (resultsList.length < 1) {
        for (let key in resultsData) {
          resultsList.push({label: resultsData[key].name, value: resultsData[key].code})
        }
      }
    }
    let date = new Date();
    let year = date.getFullYear();
    if (years.length < 1) {
      for (let i = 0; i < 20; i++) {
        years.push({key: i, label: year + i - 10, value: year + i - 10})
      }
    }
    const {getFieldProps, getFieldError} = this.props.form;
    return (
      <form>
        <div>
          <ul className={styles.ul}>
            <li>
              <Picker
                data={Array.from([orgLists])}
                title="选择党组织"
                cascade={false}
                extra="请选择党组织"
                value={this.state.org}
                onChange={v => this.setState({org: v})}
                onOk={this.orgClick}
              >
                <List.Item arrow="horizontal">选择党组织</List.Item>
              </Picker>
            </li>
            <li>
               <Picker
                  data={Array.from([menberListss])}
                  title="选择党员"
                  cascade={false}
                  extra="请选择党员"
                  value={this.state.menber}
                  onChange={v => this.setState({menber: v})}
                  onOk={v => this.setState({menber: v})}
                >
                  <List.Item arrow="horizontal">选择党员</List.Item>
               </Picker>
            </li>
            <li>
              <Picker
                data={Array.from([years])}
                title="评议年度"
                cascade={false}
                extra="请选择评议年度"
                value={this.state.years}
                onChange={v => this.setState({years: v})}
                onOk={v => this.setState({years: v})}
              >
                <List.Item arrow="horizontal">评议年度</List.Item>
              </Picker>
            </li>
            <li>
              <InputItem
                placeholder="时间格式 YYYY-MM-DD"
                error={!!getFieldError('review_time')}
                {...getFieldProps('review_time', {
                  initialValue: this.state.idt,
                  rules: [{
                    required: true, message: '必填',
                  },
                    {validator: this.validateIdp},
                  ],
                })}
              >评议日期</InputItem>
            </li>
            <li>
              <Picker
                data={Array.from([penalty])}
                title="奖惩情况"
                cascade={false}
                extra="请选择奖惩情况"
                value={this.state.sdengci}
                onChange={v => this.setState({sdengci: v})}
                onOk={v => this.setState({sdengci: v})}
              >
                <List.Item arrow="horizontal">奖惩情况</List.Item>
              </Picker>
            </li>
            <li>
              <Picker
                data={Array.from([resultsList])}
                title="评议结果"
                cascade={false}
                extra="请选择评议结果"
                value={this.state.sresults}
                onChange={v => this.setState({sresults: v})}
                onOk={v => this.setState({sresults: v})}
              >
                <List.Item arrow="horizontal">评议结果</List.Item>
              </Picker>
            </li>
          </ul>
          <WhiteSpace size="xl"/>
          <WhiteSpace size="xl"/>
          <Button onClick={this.onSubmit}
                  style={{width: '80%', backgroundColor: '#ff2c2c', color: '#ffffff', marginLeft: '10%'}}>保存</Button>
        </div>
      </form>
    )
  }
}
const TestWrapper = createForm()(addappraisal);
function mapStateToProps({addappraisals,partybuildings,organizationss}) {
   return {
      list: addappraisals.rewpun,
      results: addappraisals.result,
      orgList: organizationss.orgBottom,
      msgData: addappraisals.Lists,
     menberList: partybuildings.menberLists,
    }
}
export default connect(mapStateToProps)(TestWrapper);
